# 3.1 Python 计算器

## 3.1.1 数字

print(17 / 3)               # 5.666666666666667
print(17 // 3)              # 5
print(17 % 3)               # 取余 2
print(5 ** 2)               # 乘方 25
# n                           # 未赋值
# print(n)                    # NameError: name 'n' is not defined

## 3.1.2 字符串

print('C:\some\name')       # C:\some
                            # ame
print(r'C:\some\name')      # C:\some\name

print("""\
Usage: thingy [OPTIONS]
     -h                        Display this usage message
     -H hostname               Hostname to connect to
""")                        # 不想包含 """ 后面的换行, 可以加上  \

print(3 * 'un' + 'ium')     # + 连接, * 进行重复 : unununium

print('Py'       'thon')    # 字符串字面值 自动连接 : Python

text = ('Put several strings within parentheses '
                 'to have them joined together.'
                 'to have them joined together.'
                 'to have them joined together.')
print(text)                 # 自动连接非常适用很长的字符串

prefix = 'Py'
print(prefix + 'thon')      # + 号连接变量和字面值

word = 'Python'

'''字符串可以被 索引
'''
print(word[0])              # P
print(word[5])              # n
print(word[-1])             # n
print(word[-2])             # o
print(word[-6])             # P

'''也可以被 切片
'''
print(word[0:2])            # Py
print(word[2:5])            # tho

'''切片的开始被包括在结果中, 
即:  s[:i] + s[i:] = s
'''
print(word[:2] + word[2:])  # Python
print(word[:4] + word[4:])  # Python

'''
 +---+---+---+---+---+---+
 | P | y | t | h | o | n |
 +---+---+---+---+---+---+
 0   1   2   3   4   5   6
-6  -5  -4  -3  -2  -1
'''
# print(word[42])             # 索引不可越界
print(word[4:42])             # 切片索引越界被自动处理 : on
print(word[42:])              # 空

# 其他字符串操作可见 Python 标准库中

## 3.1.3. 列表
'''复合 数据类型
'''
squares = [1, 4, 9, 16, 25]                     # 可以包含不同类型, 但通常使用时使用相同类型
print(squares)
print(squares[0])                               # 切片       : 1
print(squares[-1])                              # 25
print(squares[-3:])                             # [9, 16, 25]
print(squares + [36, 49,223,343])               # 拼接

cubes = [1,8, 27,  65, 125]
cubes[3] = 64
print(cubes)                                    # 列表是 mutable 类型, 内容可变 : [1, 8, 27, 64, 125]
cubes.append(216)                               # 添加新元素
print(cubes)                                    # [1, 8, 27, 64, 125, 216]

letters = ['a', 'b', 'd', 'c', 'e', 'f']        # 切片也可赋值
letters[2:5] = ['D', 'C', 'E']
print(letters)

a = ['a', 'b', 'c']
n = [1, 2, 3]
x = [a, n]
print(x)                                        # 列表嵌套 : [['a', 'b', 'c'], [1, 2, 3]]
print(x[0])                                     # ['a', 'b', 'c']
print(x[0][1])                                  # b


# 3.2. 走向编程的第一步
a, b = 0, 1                                     # 多重赋值